﻿using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace DisableCup
{
    public partial class Form1 : Form
    {
        ocmctr.UmfCtr addumf = new  ocmctr.UmfCtr();//制卡
        bool devisOpen = false;
        bool running = false;
        Point mouseOff;
        bool canMove = false;
        FileStream historyfile;
        string Current;
        object queryNo="";
        string historyPath;
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
            this.FormClosing += Form1_FormClosing;
            tab.Selecting += tab_Selecting;
            CheckForIllegalCrossThreadCalls = false;
            init();
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (historyfile!=null) historyfile.Close();
        }

        private void init() {
            Current = Directory.GetCurrentDirectory();
            historyPath = Current + "/history.txt";//获取当前根目录
            historyfile = File.Open(historyPath, FileMode.Append);
        }


        void tab_Selecting(object sender, TabControlCancelEventArgs e)
        {
            closeDev(tab.SelectedIndex, e);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            header.MouseDown += Form1_MouseDown;
            header.MouseUp += Form1_MouseUp;
            header.MouseMove += Form1_MouseMove;
            close.MouseEnter += close_MouseEnter;
            close.MouseLeave += close_MouseLeave;
        }
        void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && canMove)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置  
                Location = mouseSet;
            }
        }

        void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                canMove = false;
            }
        }

        void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y);
                canMove = true;
            }
        }

        void close_MouseLeave(object sender, EventArgs e)
        {
            close.Image = Properties.Resources.close;
        }

        void close_MouseEnter(object sender, EventArgs e)
        {
            close.Image = Properties.Resources.close_f;
        }

        private void close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void clearCardBtn_Click(object sender, EventArgs e)
        {
            if (!devisOpen) {
                addstate("请先打开设备"); 
                statetolast(); return; 
            }
            if (DialogResult.Yes == MessageBox.Show(this, "确定要清除柜子的使用记录？", "", MessageBoxButtons.YesNo))
            {
                int ret = addumf.clearCup();
                if (ret == 0) addstate("清卡成功");
                else addstate("清卡失败");
            }
        }

        //制卡
        private void openDevBtn_Click(object sender, EventArgs e)
        {
            bool state = false;
            int retryNum = 3;
            if (!devisOpen)
            {
                while (retryNum-- > 0 && !state)
                {
                    state = addumf.openDev(0, 0, 0);
                }
                if (state)
                {
                    devisOpen = true;
                    openDevBtn.Text = "关闭设备";
                    addstate("设备打开成功");
                }
                else {
                    addstate("设备打开失败 请重试");
                }
            }
            else {
                try
                {
                    running = false;
                    addumf.closeDev();    
                }catch{}
                devisOpen = false;
                openDevBtn.Text = "打开设备";
                addstate("设备已关闭");
            }
        }

        private void closeDev(int tabindex, TabControlCancelEventArgs e)
        {
            try
            {
                addumf.closeDev();
                devisOpen = false;
                openDevBtn.Text = "打开设备";
                addstate("设备已关闭");
            }
            catch { }
        }
        private int DateTimeToStamp(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
        private void addstate(object msg) {
            stateListBox.Items.Add(msg);
            statetolast();
        }
        private void statetolast() {
            stateListBox.TopIndex = stateListBox.Items.Count - 1;
        }

        private void clearStateBtn_Click(object sender, EventArgs e)
        {
            stateListBox.Items.Clear();
        }

        private void checkNoBtn_Click(object sender, EventArgs e)
        {
            historyfile.Close();
            System.Diagnostics.Process.Start(historyPath);
        }

        void tbCardNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
            else { 
                
            }
        }

        private void tbCardNo_TextChanged(object sender, EventArgs e)
        {
        }

        //读取查询卡
        private void button1_Click(object sender, EventArgs e)
        {
            int ret = addumf.getCardNo(ref queryNo);
            if (ret == 0)
            {
                addstate("读取成功:" + queryNo);
            }
            else {
                addstate("读取失败");
            }
        }

        private void newCardBtn_Click(object sender, EventArgs e)
        {   
            int ret = addumf.disableCup();
            if (ret == 0)
            {
                addstate("设置成功");
            }
            else
            {
                addstate("设置失败");
            }
        }

    }
}
